package com.jinghaizhao.gqgl.user.service.impl;

import com.jinghaizhao.gqgl.login.entity.MenuLevelEntity;
import com.jinghaizhao.gqgl.mybatis.entity.PtMenuEntity;
import com.jinghaizhao.gqgl.user.service.UserService;
import com.jinghaizhao.gqgl.mybatis.entity.PtUserEntity;
import com.jinghaizhao.gqgl.user.entity.SearchUserEntity;
import com.jinghaizhao.gqgl.user.repository.UserDao;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service("userService")
public class UserServiceImpl implements UserService {
    private final static  Integer MENU_FIRST = 1;
    @Resource
    private UserDao userDao;

    @Override
    public List<PtUserEntity> queryUserInfoListByName(SearchUserEntity search) {
        return userDao.queryUserInfoListByName(search);
    }

    @Override
    public void deleteUserInfoByIds(String ids) throws UncategorizedSQLException {
        String[] array = ids.split(",");
        for(String id: array){
            userDao.deleteUserInfoByIds(id);
        }

    }

    @Override
    public PtUserEntity queryUserInfoByName(String userName) {
        return userDao.queryUserInfoByName(userName);
    }

    @Override
    public List<PtMenuEntity> queryMenuList() {
        Locale locale = LocaleContextHolder.getLocale();

        return userDao.queryMenuListByLang(locale.toString());
    }

    @Override
    public List<MenuLevelEntity> queryMenuLevelList() {
        List<MenuLevelEntity> menuLevelEntityList = new ArrayList<>();
        List<PtMenuEntity> ptMenuEntityList = queryMenuList();
        MenuLevelEntity menuLevelEntity1;
        MenuLevelEntity menuLevelEntity2;
        MenuLevelEntity menuLevelEntity3;
        for(PtMenuEntity menu1 : ptMenuEntityList){
            // 一级菜单
            if(MENU_FIRST.equals(menu1.getMenuLevel())){
                menuLevelEntity1 = new MenuLevelEntity(menu1);
                menuLevelEntityList.add(menuLevelEntity1);
                for(PtMenuEntity menu2 : ptMenuEntityList){
                    //二级菜单
                    if(menuLevelEntity1.getMenuId().equals(menu2.getParentId())){
                        menuLevelEntity2 = new MenuLevelEntity(menu2);
                        menuLevelEntity1.addChildren(menuLevelEntity2);
                        //三级菜单
                        for(PtMenuEntity menu3 : ptMenuEntityList){
                            if(menuLevelEntity2.getMenuId().equals(menu3.getParentId())){
                                menuLevelEntity3 = new MenuLevelEntity(menu3);
                                menuLevelEntity2.addChildren(menuLevelEntity3);

                            }
                        }
                    }
                }
            }
        }
        return menuLevelEntityList;
    }
}

